﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Ext.Net;

namespace KamLib.Controls
{

    public class GroupTabPanel : global::Ext.Net.GroupTabPanel
    {

        public void AddGroupTabs(IEnumerable<PageNode> pages)
        {
            foreach (var page in pages)
            {
                Groups.Add(NewGroupTab(page));
            }
        }

        public GroupTab AddGroupTab(PageNode page)
        {
            var tab = NewGroupTab(page);
            Groups.Add(tab);
            return tab;
        }

        protected GroupTab NewGroupTab(PageNode page)
        {
            var groupTab = new GroupTab { Expanded = !page.Collapsed, };

            //groupTab.Listeners.TabChange.Handler = "Ext.fly(item.tabEl)[item.mainItem == tab ? 'removeClass' : 'addClass']('x-grouptabs-has-active-item');";

            var gpanel = new Panel
            {
                Title = page.TabTitle ?? page.Title,
                TabTip = page.TabTip,
                Cls = "dsh-items-view",
                PaddingSummary = "20px 40px 20px 40px",
                Layout = "Fit",

                ContentControls =
                {
                    new LiteralControl(
                        @"<div class=""dsh-item-wrap2d""><div class=""body-wrap""><h1 class=""page-header"">" + (page.PageTitle ?? page.Title) +
                        @"</h1><div class=""column3"">" + page.Desc + @"</div></div>"
                    ),
                    new Dashboard
                    {
                        ID = page.ID + "_Dsh" + page.Index, 
                        ButtonKind = DashboardButtonKind.Detail,
                        DataSource = page, 
                    },
                    new LiteralControl(@"</div>"),
                },
            };
            groupTab.Items.Add(gpanel);

            foreach (var p in page.Items)
            {
                if (p.Url.isnull() || p.ID.isnull()) continue;

                var lpanel = (UserControl)Page.LoadControl(p.Url);
                lpanel.ID = p.ID;

                var panel = new PagePanel
                {
                    PageNode = p,
                    Title = p.TabTitle ?? p.Title,
                    TabTip = p.TabTip ?? p.TabTitle ?? p.Title,
                    IconCls = "icon-" + p.Icon + "-24",
                    ContentControls = { lpanel },
                };

                groupTab.Items.Add(panel);
            }

            return groupTab;
        }


    }
}
